package com.kanche.bigbaby.collection.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.Guard;
import com.kanche.bigbaby.collection.domain.ApiListResult;
import com.kanche.bigbaby.collection.domain.Vehicle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

//import com.kkche.bss.R;
//import com.kkche.bss.collection.domain.ApiListResult;
//import com.kkche.bss.collection.domain.Vehicle;
//import com.kkche.bss.dialogs.KancheDialogFragment;
//import com.kkche.bss.utils.Guard;

//import com.kkche.merchant.R;
//import com.kkche.merchant.domain.ApiListResult;
//import com.kkche.merchant.utils.Guard;

/**
 * Created by nealmi on 6/10/14.
 */
public class VinRecognitionDialog extends KancheDialogFragment implements AdapterView.OnItemClickListener {


    private ListView listView;
    private ProgressBar progressBar;
    private TextView textView;
    private String key;
    private OnChoiceMadeListener listener;
    private String value;

    public VinRecognitionDialog() {
    }

//    public VinRecognitionDialog(OnChoiceMadeListener listener) {
//        this.listener = listener;
//    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public OnChoiceMadeListener getListener() {
        return listener;
    }

    public void setListener(OnChoiceMadeListener listener) {
        this.listener = listener;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Vehicle.Spec spec = (Vehicle.Spec) parent.getItemAtPosition(position);
        if (listener != null) {
            listener.onSelected(getKey(), spec);
        }
        dismiss();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialog_vin_recognition, container);
        listView = (ListView) view.findViewById(R.id.listView);
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        textView = (TextView) view.findViewById(R.id.textView);
        textView.setText("" + getValue() + " 识别中...");
        listView.setOnItemClickListener(this);
        getDialog().setTitle("VIN识别");

        loadData();
        return view;
    }

    private void loadData() {
        progressBar.setVisibility(View.VISIBLE);
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("vin", getValue());
        getBSSService().findVehiclesByVinRecognition(parameters, new Callback<ApiListResult>() {
            @Override
            public void success(ApiListResult listResult, Response response) {
                listView.setAdapter(new VinRecognitionResultAdapter(getActivity(), listResult.getVehicleSpecs()));
                progressBar.setVisibility(View.GONE);
                if (listResult.getVehicleSpecs().size() == 0 || listResult.getVehicleSpecs().get(0).getDisplayShort() == "") {
                    textView.setText("未识别出此vin码");
                } else {
                    textView.setText("" + getValue() + " 识别完成(" + listResult.getVehicleSpecs().size() + "个结果)");
                }

            }

            @Override
            public void failure(RetrofitError error) {
                progressBar.setVisibility(View.GONE);
                if(null == error) return;
                Guard.handleError(getActivity(), error);

            }
        });
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public interface OnChoiceMadeListener {
        void onSelected(String key, Vehicle.Spec spec);
    }

    private class VinRecognitionResultAdapter extends BaseAdapter {
        private Context context;
        private List<Vehicle.Spec> specs;

        public VinRecognitionResultAdapter(Context context) {
            this.context = context;
            specs = new ArrayList<Vehicle.Spec>();
        }


        public VinRecognitionResultAdapter(Context context, List<Vehicle.Spec> specs) {
            this.context = context;
            this.specs = specs;
        }


        public void refresh(List<Vehicle.Spec> specs) {
            if (specs == null) return;
            this.specs = specs;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return specs.size();
        }

        @Override
        public Object getItem(int position) {
            return specs.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(context);
                convertView = inflater.inflate(R.layout.dialog_vin_recognition_list_item, null);
                holder = new Holder();
                holder.title = (TextView) convertView.findViewById(R.id.title);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            Vehicle.Spec spec = (Vehicle.Spec) getItem(position);
            holder.title.setText(spec.getDisplayValue());
            return convertView;
        }


    }

    private class Holder {
        public TextView title;
    }
}